package com.cy.controller;


import com.cy.common.ResultResponse;
import com.cy.config.RequiresRole;
import com.cy.entity.UserCountByLastname;
import com.cy.entity.Users;
import com.cy.entity.vo.UserVo;
import com.cy.service.IUsersService;
import com.cy.service.impl.UsersServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  用户控制层
 * </p>
 *
 * @author Aleven
 * @since 2024-12-21
 */
@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    UsersServiceImpl usersService;

    @Operation(summary = "用户登录")
    @PostMapping("/UserLogin")
    ResultResponse UserLogin(@RequestBody UserVo userVo, HttpServletRequest request) throws Exception {
        return usersService.UserLogin(userVo);
    }

    @Operation(summary = "用户注册")
    @PostMapping("/UserRegister")
    ResultResponse UserRegister(@RequestBody Users users) throws Exception {
        return usersService.UserRegister(users);
    }

    @Operation(summary = "重置密码")
    @PutMapping("/forgetPwd")
    ResultResponse forgetPwd(@RequestBody UserVo userVo) throws Exception {
        return usersService.forgetPwd(userVo);
    }


    @Operation(summary = "查询所有用户信息")
    @GetMapping ("/findusersAll")
    @RequiresRole("2")
    public List<Users> findusersAll(){
        return usersService.findusersAll();
    }



    @Operation(summary = "根据用户ID查询")
    @GetMapping ("/findusersById")
    public ResultResponse findusersById(String UserID){
        Users users = usersService.findusersById(UserID);
        return ResultResponse.success(users);
    }



    @Operation(summary = "修改用户信息")
    @PostMapping("/updateUsers")
    public ResultResponse updateUsers(@RequestBody Users user){
        int n = usersService.updateUsers(user);
        return n>0?ResultResponse.success():ResultResponse.fail();
    }


    @Operation(summary = "删除用户")
    @DeleteMapping("/deleteUsers")
    @RequiresRole("2")
    public ResultResponse deleteUsers(Integer UserId){
        int n = usersService.deleteUsers(UserId);
        return n>0?ResultResponse.success():ResultResponse.fail();
    }

    @Operation(summary = "统计用户")
    @GetMapping ("/countUsers")
    @RequiresRole("2")
    public ResultResponse countUsers(){
        Integer countUsers = usersService.countUsers();
        return ResultResponse.success(countUsers);
    }
    @Operation(summary = "查询相同姓氏的用户并统计其数量")
    @GetMapping ("/countUsersByLastname")
    public ResultResponse countUsersByLastname(String surname){
        List<UserCountByLastname> list = usersService.countUsersByLastname(surname);
        return ResultResponse.success(list,list.size());
    }
}
